/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
export var findPairs = function (nums, k) {
  const set1 = new Set()
  const set2 = new Set()
  for (let i = 0; i < nums.length; i++) {
    const num = nums[i]
    const diffA = num - k
    const diffB = num + k
    if (set1.has(diffA)) {
      const key = num > diffA ? `${num}-${diffA}` : `${diffA}-${num}`
      set2.add(key)
    }
    if (set1.has(diffB)) {
      const key = num > diffB ? `${num}-${diffB}` : `${diffB}-${num}`
      set2.add(key)
    }
    set1.add(num)
  }
  return set2.size
}
